﻿//using OpenPasteSpider.Localization;
using Microsoft.AspNetCore.Http;
using System;
using System.Reflection;
using Volo.Abp.Application.Services;

namespace OpenPasteSpider
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class OpenPasteSpiderAppService : ApplicationService
    {

        /// <summary>
        /// 
        /// </summary>
        protected IOpenPasteSpiderDbContext _dbContext => LazyServiceProvider.LazyGetRequiredService<OpenPasteSpiderDbContext>();

        /// <summary>
        /// 
        /// </summary>
        protected OpenPasteSpiderAppService()
        {
            //LocalizationResource = typeof(OpenPasteSpiderResource);
            ObjectMapperContext = typeof(OpenPasteSpiderApplicationModule);
        }

        /// <summary>
        /// 
        /// </summary>
        private HttpContext _context { get; set; } = null;

        /// <summary>
        /// 
        /// </summary>
        protected HttpContext _httpContext
        {
            get
            {
                if (_context != null)
                {
                    return _context;
                }
                var _http = LazyServiceProvider.LazyGetRequiredService<IHttpContextAccessor>();
                _context = _http.HttpContext;
                return _context;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="obj"></param>
        protected void DefaultBeforeCreate(PropertyInfo[] properties, object obj)
        {
            if (properties != null && properties.Length > 0)
            {
                foreach (var item in properties)
                {
                    switch (item.Name)
                    {
                        case "UserId":
                            {
                                var user_id = ReadLoginUserId();
                                item.SetValue(obj, user_id);
                            }
                            break;
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="obj"></param>
        protected void DefaultBeforeUpdate(PropertyInfo[] properties, object obj)
        {
            if (properties != null && properties.Length > 0)
            {
                foreach (var item in properties)
                {
                    switch (item.Name)
                    {
                        case "UpdateDate":
                            {
                                item.SetValue(obj, DateTime.Now);
                            }
                            break;
                        case "UpdateTime":
                            {
                                item.SetValue(obj, DateTime.Now);
                            }
                            break;
                    }
                }
            }
        }


        //}



        //public abstract class UserAppService : ApplicationService
        //{
        //    //private readonly IHttpContextAccessor _httpContextAccessor;

        //    private readonly HttpContext _httpContext;

        //    protected UserAppService(IHttpContextAccessor httpContextAccessor)
        //    {
        //        ObjectMapperContext = typeof(OpenPasteSpiderApplicationModule);
        //        _httpContext = httpContextAccessor.HttpContext;
        //    }

        /// <summary>
        /// 获取当前登录用户
        /// </summary>
        /// <returns></returns>
        protected int ReadLoginUserId()
        {
            var token = _httpContext.Request.Headers["token"].ToString();
            if (!string.IsNullOrEmpty(token))
            {
                var strs = token.Split("_");
                int.TryParse(strs[0], out var userid);
                return userid;
            }
            return 0;
        }

        /// <summary>
        /// 获取当前登录用户的密钥 过滤器中已经校验过了，这里只需要读取
        /// </summary>
        /// <returns></returns>
        protected string ReadLoginUserToken()
        {
            var token = _httpContext.Request.Headers["token"].ToString();
            if (!string.IsNullOrEmpty(token))
            {
                return token;
            }
            return "";
        }

        /// <summary>
        /// 读取客户端IP
        /// </summary>
        /// <returns></returns>
        protected string ReadClientIP()
        {
            string result = String.Empty;
            result = _httpContext.Request.Headers["X-Forwarded-For"];
            if (String.IsNullOrEmpty(result))
            {
                result = _httpContext.Connection.RemoteIpAddress.ToString();
            }
            if (String.IsNullOrEmpty(result))
            {
                result = _httpContext.Request.Headers["REMOTE_ADDR"];
            }

            if (result != null)
            {
                if (result.Contains(","))
                {
                    result = result.Split(',')[0];
                }
                else if (result == "::1")
                {
                    result = "127.0.0.1";
                }
            }
            return result;
        }
    }
}
